<template>
	<view class="register-container">
		<view class="logo-box">
			<image class="logo" src="/static/logo.png" mode="aspectFit"></image>
			<text class="title">棋牌室预约系统</text>
		</view>
		
		<view class="form-box">
			<view class="input-group">
				<text class="label">邮箱</text>
				<input class="input" type="text" v-model="email" placeholder="请输入邮箱地址" />
			</view>
			
			<view class="input-group">
				<text class="label">验证码</text>
				<input class="input" type="text" v-model="code" placeholder="请输入验证码" maxlength="6" />
				<view :class="['code-btn', sendingCode ? 'disabled' : '']" @click="sendVerificationCode">
					{{ codeBtnText }}
				</view>
			</view>
			
			<view class="input-group">
				<text class="label">昵称</text>
				<input class="input" type="text" v-model="nickname" placeholder="请输入昵称" />
			</view>
			
			<view class="input-group">
				<text class="label">密码 (选填)</text>
				<input class="input" type="password" v-model="password" placeholder="请设置登录密码" />
			</view>
			
			<view class="agreement">
				<checkbox :checked="agreed" @tap="agreed = !agreed" color="#7FB992" style="transform:scale(0.7);" />
				<text class="agreement-text">我已阅读并同意<text class="link">《用户协议》</text>和<text class="link">《隐私政策》</text></text>
			</view>
			
			<button class="register-btn" @click="handleRegister">立即注册</button>
			
			<view class="login-link">
				已有账号？<text class="link" @click="goToLogin">返回登录</text>
			</view>
		</view>
	</view>
</template>

<script>
import request from '@/utils/request.js';
import { setToken, setUserInfo } from '@/utils/auth.js';

export default {
	data() {
		return {
			email: '',
			code: '',
			nickname: '',
			password: '',
			agreed: false,
			sendingCode: false,
			countdown: 60,
			timer: null
		}
	},
	computed: {
		codeBtnText() {
			return this.sendingCode ? `${this.countdown}秒` : '获取验证码';
		}
	},
	methods: {
		/**
		 * 发送验证码
		 */
		async sendVerificationCode() {
			// 验证邮箱格式
			if (!this.validateEmail(this.email)) {
				uni.showToast({
					title: '请输入正确的邮箱地址',
					icon: 'none'
				});
				return;
			}
			
			// 防止重复发送
			if (this.sendingCode) {
				return;
			}
			
			try {
				this.sendingCode = true;
				
				// 调用发送验证码接口
				const res = await request.post('/api/mobile/user/email/verify', {
					email: this.email,
					type: 'register'
				});
				
				uni.showToast({
					title: '验证码已发送',
					icon: 'success'
				});
				
				// 开始倒计时
				this.startCountdown();
			} catch (error) {
				this.sendingCode = false;
				uni.showToast({
					title: '验证码发送失败',
					icon: 'none'
				});
			}
		},
		
		/**
		 * 验证码倒计时
		 */
		startCountdown() {
			this.countdown = 60;
			this.timer = setInterval(() => {
				if (this.countdown > 0) {
					this.countdown--;
				} else {
					this.sendingCode = false;
					clearInterval(this.timer);
				}
			}, 1000);
		},
		
		/**
		 * 处理注册
		 */
		async handleRegister() {
			// 表单校验
			if (!this.validateEmail(this.email)) {
				uni.showToast({
					title: '请输入正确的邮箱地址',
					icon: 'none'
				});
				return;
			}
			
			if (!this.code || this.code.length !== 6) {
				uni.showToast({
					title: '请输入6位验证码',
					icon: 'none'
				});
				return;
			}
			
			if (!this.nickname) {
				uni.showToast({
					title: '请输入昵称',
					icon: 'none'
				});
				return;
			}
			
			if (this.password && this.password.length < 6) {
				uni.showToast({
					title: '密码长度不能少于6位',
					icon: 'none'
				});
				return;
			}
			
			if (!this.agreed) {
				uni.showToast({
					title: '请先阅读并同意用户协议',
					icon: 'none'
				});
				return;
			}
			
			try {
				uni.showLoading({
					title: '注册中...'
				});
				
				// 构建注册参数
				const params = {
					email: this.email,
					code: this.code,
					nickname: this.nickname
				};
				
				// 如果设置了密码，加入参数
				if (this.password) {
					params.password = this.password;
				}
				
				// 调用注册接口
				const res = await request.post('/api/mobile/user/register', params);
				
				// 保存登录信息
				setToken(res.data.token);
				setUserInfo(res.data.userInfo);
				
				uni.hideLoading();
				uni.showToast({
					title: '注册成功',
					icon: 'success'
				});
				
				// 注册成功后跳转到首页
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index/index'
					});
				}, 1500);
			} catch (error) {
				uni.hideLoading();
			}
		},
		
		/**
		 * 跳转到登录页面
		 */
		goToLogin() {
			uni.navigateTo({
				url: '/pages/login/login'
			});
		},
		
		/**
		 * 验证邮箱格式
		 */
		validateEmail(email) {
			const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
			return reg.test(email);
		}
	},
	onUnload() {
		// 页面卸载时清除定时器
		if (this.timer) {
			clearInterval(this.timer);
		}
	}
}
</script>

<style lang="scss">
.register-container {
	min-height: 100vh;
	padding: 50rpx;
	background-color: #f8f8f8;
	display: flex;
	flex-direction: column;
	
	.logo-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 40rpx;
		margin-bottom: 60rpx;
		
		.logo {
			width: 160rpx;
			height: 160rpx;
		}
		
		.title {
			margin-top: 30rpx;
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
		}
	}
	
	.form-box {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 40rpx 30rpx;
		box-shadow: 0 0 20rpx rgba(0,0,0,0.05);
		
		.input-group {
			position: relative;
			margin-bottom: 30rpx;
			
			.label {
				display: block;
				font-size: 28rpx;
				color: #666;
				margin-bottom: 15rpx;
			}
			
			.input {
				width: 100%;
				height: 90rpx;
				background-color: #f5f5f5;
				border-radius: 10rpx;
				padding: 0 20rpx;
				font-size: 30rpx;
				color: #333;
			}
			
			.code-btn {
				position: absolute;
				right: 20rpx;
				bottom: 25rpx;
				font-size: 28rpx;
				color: #7FB992;
				
				&.disabled {
					color: #999;
				}
			}
		}
		
		.agreement {
			display: flex;
			align-items: center;
			margin: 30rpx 0;
			
			.agreement-text {
				font-size: 26rpx;
				color: #666;
				
				.link {
					color: #7FB992;
				}
			}
		}
		
		.register-btn {
			width: 100%;
			height: 90rpx;
			background-color: #7FB992;
			color: #fff;
			border-radius: 10rpx;
			font-size: 32rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 40rpx;
			margin-bottom: 40rpx;
		}
		
		.login-link {
			text-align: center;
			font-size: 28rpx;
			color: #666;
			
			.link {
				color: #7FB992;
			}
		}
	}
}
</style>